<?php

class IndexController extends Edm_Controller_Action {
    public function indexAction () {
        $this->view->title = '首页';
        $this->view->catList = $this->_articlecat->getArticleCat(0, 1, array('cat_id', 'cat_name'));
        $this->view->headLink()->appendStylesheet('/css/common/pager.css');
        $filter = new Zend_Filter_HtmlEntities();
        $data = array(
                'pageSize' => $this->common->defaultPageSize,
                'pageNum' => $filter->filter($this->_request->get('page', 1)),
                'cat_id' => $filter->filter($this->_request->get('cat_id', null)),
                'keywords' => $filter->filter($this->_request->get('keywords', null))
        );
        $res = $this->_article->articleList($data);

        $this->view->search = array('cat_id' => $data['cat_id'], 'keywords' => $data['keywords']);
        $this->view->list = $res['list'];
        $this->pager->Pager($res['count'], $data['pageSize']);
        $this->view->pageResult = $this->pager->display(2, 'badoo');
    }

    /* 文章详细内容 */
    public function detailAction() {
        $article_id = $this->_request->get('article_id');
        /* 缓存 */
        $frontendOptions=array(
                'lifeTime' => 7200,
                'automatic_serialization' => true
        );
        $backendOptions=array(
                'cache_dir'=> $this->view->cacheDir
        );
        $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

        if (is_numeric($article_id)) {
            if (!$res = $cache->load('article_id' . $article_id)) {
                $res = $this->_article->getArticle($article_id);
                $cache->save($res, 'article_id' . $article_id);
            }

            $this->view->title = $res['title'];
            $res['content'] = htmlspecialchars_decode($res['content']);
            $this->view->article = $res;
            $this->view->headMeta()->appendName('keywords', $res['keywords']);

            /* 相关文章 （DB处应另写方法，此处省略） */
            $data = array('cat_id' => $res['cat_id'], 'pageSize' => 10, 'cols' => array('a.article_id', 'a.title'));
            if (!$list = $cache->load('article_list')) {
                $list = $this->_article->articleList($data);
                $cache->save($list, 'article_list');
            }

            $this->view->list = $list['list'];
        }
    }

    /* 验证码 */
    public function authimageAction() {

        $this->getFrontController()->setParam('noViewRenderer', true);
        /* 禁用layout 否则不能显示验证码 */
        $this->_helper->layout->disableLayout();

        $imagecode = new Edm_Controller_Plugin_AuthImage();
        //返回验证码图片
        $imagecode->image2();
    }

}
